package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.LanGoodsEvaluateReq;
import com.cloud.lan.api.dto.resp.LanUserResp;
import com.cloud.lan.config.cache.LoadingCacheConfiguration;
import com.cloud.lan.dao.LanBadgeCfgMapper;
import com.cloud.lan.dao.LanGoodsEvaluateMapper;
import com.cloud.lan.dao.LanUserMapper;
import com.cloud.lan.dao.LanUserPraiseMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.enums.LanError;
import com.cloud.lan.entity.model.dataobject.LanGoodsEvaluateDO;
import com.cloud.lan.entity.model.dataobject.LanUserDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanGoodsEvaluateService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ZERO;

/**
 * <p>
 * 商品评价 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2022-05-21
 */
@Slf4j
@Service
public class LanGoodsEvaluateServiceImpl extends ServiceImpl<LanGoodsEvaluateMapper, LanGoodsEvaluateDO> implements ILanGoodsEvaluateService {

    @Resource
    LanUserMapper lanUserMapper;
    @Resource
    LanGoodsEvaluateMapper lanGoodsEvaluateMapper;
    @Resource
    LanUserPraiseMapper lanUserPraiseMapper;
    @Resource
    LoadingCacheConfiguration cacheConfiguration;
    @Resource
    LanBadgeCfgMapper lanBadgeCfgMapper;

    @Override
    public BaseResponse getGoodsEvaluate(LanGoodsEvaluateReq req) {
        log.info("#查询 用户点赞 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getGoodsId())},
                new String[]{"goodsId"}, LanError.ILLEGAL_PARAMS.getCode());
        LambdaQueryWrapper<LanGoodsEvaluateDO> qw = new LambdaQueryWrapper<>();
        qw.eq(LanGoodsEvaluateDO::getGoodsId, req.getGoodsId());
        qw.orderByDesc(LanGoodsEvaluateDO::getSort).orderByDesc(LanGoodsEvaluateDO::getCreateTime);
        Page<LanGoodsEvaluateDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        if (req.getId() != null) {
            qw.eq(LanGoodsEvaluateDO::getId, req.getId());
        }
        IPage<LanGoodsEvaluateDO> list = lanGoodsEvaluateMapper.selectPage(page, qw);
        LanUserResp lanUserResp = cacheConfiguration.userInfo.getUnchecked(req.getToken());
        list.getRecords().forEach(e -> {
            LanUserDO lanUserDO = lanUserMapper.getUserId(e.getUserId());
            e.setOneself(lanUserPraiseMapper.getOneselfCount(e.getId(), lanUserResp.getUserId()) > ZERO.getKey());
            e.setWxName(lanUserDO.getWxName());
            e.setWxHead(lanUserDO.getWxHead());
            e.setPraiseCount(lanUserPraiseMapper.getPraiseCount(e.getId()));
            e.setLanBadgeCfgDO(lanBadgeCfgMapper.getBadgeByUserId(e.getUserId()));
        });
        return BaseResponse.success(list);
    }

    @Override
    public BaseResponse addEvaluate(LanGoodsEvaluateReq req) {
        log.info("#新增 商品评价 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getGoodsId()), req.getTitle(), req.getContent()},
                new String[]{"goodsId", "title", "content"}, LanError.ILLEGAL_PARAMS.getCode());
        LanUserResp lanUserResp = cacheConfiguration.userInfo.getUnchecked(req.getToken());
        LanGoodsEvaluateDO lanGoodsEvaluateDO = new LanGoodsEvaluateDO();
        BeanUtil.copyProperties(req, lanGoodsEvaluateDO);
        lanGoodsEvaluateDO.setUserId(lanUserResp.getUserId());
        lanGoodsEvaluateMapper.insert(lanGoodsEvaluateDO);
        return BaseResponse.success();
    }
}
